package com.itheima.controller;

import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/brand")
public class BrandController extends HttpServlet {
    private BrandService brandService = new BrandServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置post请求编码
        req.setCharacterEncoding("UTF-8");
        String action = req.getParameter("action");
        switch (action){
            case "selectAll":
                this.selectAll(req,resp);
                break;
            case "add":
                this.add(req,resp);
                break;
            case "findById":
                this.findById(req,resp);
                break;
            case "update":
                this.update(req,resp);
                break;
            case "delectById":
                this.delectById(req,resp);
                break;
        }
    }

    private void delectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String id = req.getParameter("id");
        //调用service中的方法
        brandService.delectById(id);
        //转发
        this.selectAll(req, resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        try {
            //设置post请求编码
            req.setCharacterEncoding("UTF-8");
            //使用BeanUtils封装表单数据
            Map<String,String[]> map = req.getParameterMap();
            Brand brand = new Brand();
            BeanUtils.populate(brand,map);
            //调用service方法
            brandService.update(brand);
            this.selectAll(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String id = req.getParameter("id");
        //调用service中的方法
        Brand brand = brandService.findById(id);
        //将查询结果写入域对象
        req.setAttribute("brand",brand);
        //转发
        req.getRequestDispatcher("/updateBrand.jsp").forward(req,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        try {

            //使用BeanUtils封装表单数据
            Map<String,String[]> map = req.getParameterMap();
            Brand brand = new Brand();
            BeanUtils.populate(brand,map);

            brandService.addBrand(brand);
            this.selectAll(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Brand> brands = brandService.selectAll();
        req.setAttribute("brands",brands);
        req.getRequestDispatcher("/bandList.jsp").forward(req,resp);

    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp){
        this.doGet(req, resp);
    }
}
